package com.tjmc.finance.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjmc.finance.service.PlatformUserAccountService;
import com.tjmc.recruit.admin.api.entity.PlatformUserInfoEntity;
import com.tjmc.recruit.admin.api.feign.RemoteUserService;
import com.tjmc.recruit.common.core.constant.CommonConstants;
import com.tjmc.recruit.common.core.constant.SecurityConstants;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;

import com.tjmc.recruit.common.security.annotation.Inner;
import com.tjmc.recruit.common.security.util.SecurityUtils;
import com.tjmc.recruit.finance.api.dto.AccountChangeDTO;
import com.tjmc.recruit.finance.api.entity.PlatformUserAccountEntity;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.tjmc.recruit.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 平台用户账户表
 *
 * @author recruit
 * @date 2024-11-25 14:18:09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/platformUserAccount")
@Tag(description = "platformUserAccount", name = "平台用户账户表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PlatformUserAccountController {

    private final PlatformUserAccountService platformUserAccountService;
    private final RemoteUserService remoteUserService;

    /**
     * 分页查询
     *
     * @param page                分页对象
     * @param platformUserAccount 平台用户账户表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R getPlatformUserAccountPage(@ParameterObject Page page, @ParameterObject PlatformUserAccountEntity platformUserAccount) {
        LambdaQueryWrapper<PlatformUserAccountEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(platformUserAccountService.page(page, wrapper));
    }


    /**
     * 通过条件查询平台用户账户表
     *
     * @param platformUserAccount 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询", description = "通过条件查询对象")
    @GetMapping("/details")
    public R getDetails(@ParameterObject PlatformUserAccountEntity platformUserAccount) {
        return R.ok(platformUserAccountService.list(Wrappers.query(platformUserAccount)));
    }

    /**
     * 新增平台用户账户表
     *
     * @param platformUserAccount 平台用户账户表
     * @return R
     */
    @Inner
    @Operation(summary = "新增平台用户账户表", description = "新增平台用户账户表")
    @SysLog("新增平台用户账户表")
    @PostMapping
    public R save(@RequestBody PlatformUserAccountEntity platformUserAccount) {
        PlatformUserAccountEntity userAccount = platformUserAccountService.getOne(new LambdaQueryWrapper<PlatformUserAccountEntity>().eq(PlatformUserAccountEntity::getCompanyId, platformUserAccount.getCompanyId()));
        if (userAccount != null) {
            return R.failed("企业账户已存在");
        }
        return R.ok(platformUserAccountService.save(platformUserAccount));
    }

    /**
     * 查询账户信息
     *
     * @param accountId 用户ID
     * @return
     */
    @Operation(summary = "查询平台用户账户", description = "查询平台用户账户")
    @SysLog("查询平台用户账户")
    @GetMapping("/{accountId}")
    @Inner
    public R getById(@PathVariable("accountId") Long accountId) {
        return R.ok(platformUserAccountService.getAccountByAccountId(accountId).getData());
    }

    /**
     * 查询指定用户账户
     *
     * @return
     */
    @Operation(summary = "查询指定用户账户", description = "查询指定用户账户")
    @SysLog("查询指定用户账户")
    @GetMapping("/byId/{accountId}")
    public R getCurrent(@PathVariable("accountId") Long accountId) {
        return R.ok(platformUserAccountService.getAccountByAccountId(accountId).getData());
    }

    /**
     * 修改平台用户账户表
     *
     * @param platformUserAccount 平台用户账户表
     * @return R
     */
    @Operation(summary = "修改平台用户账户表", description = "修改平台用户账户表")
    @SysLog("修改平台用户账户表")
    @PutMapping
    public R updateById(@RequestBody PlatformUserAccountEntity platformUserAccount) {
        return R.ok(platformUserAccountService.updateById(platformUserAccount));
    }

    /**
     * 通过id删除平台用户账户表
     *
     * @param ids accountId列表
     * @return R
     */
    @Operation(summary = "通过id删除平台用户账户表", description = "通过id删除平台用户账户表")
    @SysLog("通过id删除平台用户账户表")
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(platformUserAccountService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 变更账户数据
     *
     * @param changeDTO 变更信息
     * @return
     */
    @Operation(summary = "账户数据变更", description = "账户数据变更")
    @SysLog("账户数据变更")
    @PutMapping("/changeAccountInfo")
    @Inner
    public R changeAccountInfo(@RequestBody AccountChangeDTO changeDTO) {
        R<Boolean> booleanR = platformUserAccountService.changeAccountInfo(changeDTO);
        if (booleanR.getCode() != CommonConstants.SUCCESS) {
            return R.failed(booleanR.getMsg());
        }
        return R.ok();
    }
}
